package fleditor.core.iso
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import fleditor.core.asset.AssetBase;
	
	public class IsoScene extends IsoSprite
	{
		protected var _assetArray:Array=[];
		private var _isAutoDepthSort:Boolean;
		private var _isDrawFloor:Boolean;
		private var _bmFloor:Bitmap;//地表层并合的bitmap
		private var _bmdFloor:BitmapData;//地表层并合的BitmapData
		
		/**
		 * Iso场景基类
		 * @param isAutoDepthSort 是否自动深度排序
		 * @param isDrawFloor 是否合并且绘画地表层
		 */
		public function IsoScene(isAutoDepthSort:Boolean=true, isDrawFloor:Boolean=false)
		{
			_isAutoDepthSort=isAutoDepthSort;
			_isDrawFloor=isDrawFloor;
		}
		
		/**
		 *增加至地图物件层
		 */		
		public function addAsset(assetBase:AssetBase):void
		{
			addChild(assetBase);
			_assetArray.push(assetBase);
			if(_isAutoDepthSort) depthSort();
		}
		
		/**
		 *从地图物件层移除
		 */		
		public function removeAsset(assetBase:AssetBase):void
		{
			if(assetBase)
			{
				if(_assetArray.indexOf(assetBase)!=-1) _assetArray.splice(_assetArray.indexOf(assetBase), 1);
				assetBase.dispose();
			}
			assetBase=null;
		}
		
		/**
		 *拼合地表层 
		 */		
		public function drawFloor():void
		{			
			if(!_isDrawFloor) return;
			
			if(_bmFloor)
			{
				if(_bmFloor.parent) _bmFloor.parent.removeChild(_bmFloor);
				if(_bmFloor.bitmapData) _bmFloor.bitmapData.dispose();
			}
			_bmFloor=null;
			
			if(_bmdFloor) _bmdFloor.dispose();
			_bmdFloor=null;
			
			_bmdFloor=new BitmapData(width, height);
			_bmFloor=new Bitmap(_bmdFloor);
			
			for each(var ab:AssetBase in _assetArray)
			{
				if(ab.isFloor)
				{
					_bmdFloor.draw(ab);
				}
			}
			removeAllFloor();
			addChildAt(_bmFloor, 0);
		}
		
		/**
		 *取消加载地表层地图资源物件 
		 */		
		private function removeAllFloor():void
		{					
			for each(var ab:AssetBase in _assetArray)
			{
				if(ab.isFloor && ab.parent) ab.parent.removeChild(ab); 
			}
		}
		
		/**
		 *深度排序 
		 */		
		public function depthSort():void
		{
			_assetArray.sortOn(["depth","isFloor"], [Array.NUMERIC, Array.DESCENDING | Array.NUMERIC]);
			for(var i:int = 0; i < _assetArray.length; i++)
			{
				setChildIndex(_assetArray[i], i);
			}
		}
		
		/**
		 *清理全部的地图资源物件
		 */		
		public function clearAll():void
		{
			for(var i:int=0;i<_assetArray.length;i++)
			{
				var mapAssetBase:IsoSprite=_assetArray[i] as IsoSprite;
				if(mapAssetBase)
				{
					if(mapAssetBase.parent) mapAssetBase.parent.removeChild(mapAssetBase);
					mapAssetBase.dispose();
				}
				mapAssetBase=null;
			}
			_assetArray=[];
			
			while(numChildren>0)
			{
				mapAssetBase=getChildAt(0) as IsoSprite;
				if(mapAssetBase)
				{
					if(mapAssetBase.parent) mapAssetBase.parent.removeChild(mapAssetBase);
					mapAssetBase.dispose();
				}
				mapAssetBase=null;
			}
		}
		
		override public function dispose():void
		{
			super.dispose();
			
			clearAll();
			
			if(_bmdFloor) _bmdFloor.dispose();
			_bmdFloor=null;
			
			if(_bmFloor)
			{
				if(_bmFloor.parent) _bmFloor.parent.removeChild(_bmFloor);
				_bmFloor.bitmapData.dispose();
			}
			_bmFloor=null;
			
			if(parent) parent.removeChild(this);
		}
	}
}